package com.papers.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.helper.LoginHelper;
import com.papers.common.utils.DateUtils;
import com.papers.common.utils.StringUtils;
import com.papers.system.domain.TJuanUserCourse;
import com.papers.system.domain.bo.TJuanUserCourseBo;
import com.papers.system.domain.vo.TJuanUserCourseVo;
import com.papers.system.mapper.TJuanUserCourseMapper;
import com.papers.system.service.ITJuanUserCourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户课程关联Service业务层处理
 *
 * @author papers
 * @date 2022-10-12
 */
@RequiredArgsConstructor
@Service
public class TJuanUserCourseServiceImpl implements ITJuanUserCourseService {

    private final TJuanUserCourseMapper baseMapper;

    /**
     * 查询用户课程关联
     */
    @Override
    public TJuanUserCourseVo queryById(String userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户课程关联列表
     */
    @Override
    public TableDataInfo<TJuanUserCourseVo> queryPageList(TJuanUserCourseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TJuanUserCourse> lqw = buildQueryWrapper(bo);
        Page<TJuanUserCourseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户课程关联列表
     */
    @Override
    public List<TJuanUserCourseVo> queryList(TJuanUserCourseBo bo) {
        LambdaQueryWrapper<TJuanUserCourse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TJuanUserCourse> buildQueryWrapper(TJuanUserCourseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TJuanUserCourse> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), TJuanUserCourse::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getCourseId()), TJuanUserCourse::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), TJuanUserCourse::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedTime() != null, TJuanUserCourse::getCreatedTime, bo.getCreatedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdatedBy()), TJuanUserCourse::getUpdatedBy, bo.getUpdatedBy());
        lqw.eq(bo.getUpdatedTime() != null, TJuanUserCourse::getUpdatedTime, bo.getUpdatedTime());
        return lqw;
    }

    /**
     * 新增用户课程关联
     */
    @Override
    public Boolean insertByBo(TJuanUserCourseBo bo) {
        TJuanUserCourse add = BeanUtil.toBean(bo, TJuanUserCourse.class);

        add.setUpdatedBy(LoginHelper.getUsername());
        add.setCreatedBy(LoginHelper.getUsername());
        add.setCreatedTime(DateUtils.getNowDate());
        add.setUpdatedTime(DateUtils.getNowDate());

        // validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户课程关联
     */
    @Override
    public Boolean updateByBo(TJuanUserCourseBo bo) {
        TJuanUserCourse update = BeanUtil.toBean(bo, TJuanUserCourse.class);
        update.setUpdatedBy(LoginHelper.getUsername());
        update.setUpdatedTime(DateUtils.getNowDate());

        // validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TJuanUserCourse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户课程关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
